﻿using System;

namespace 多态
{
    /* 概念：让一个对象能够表现出多种状态（类型）
	 * 实现多态的3种手段：
	 * 1.虚方法
	 * virtual将父类的方法标记为虚方法；再将子类的同名方法标记override
	 * 调用的还是父类的，执行子类重写的对象
	 * 2.抽象类
	 * 3.接口
	 */

    internal class Program
    {
        private static void Main(string[] args)
        {
            Chinese cn1 = new Chinese("韩美");
            Chinese cn2 = new Chinese("李四");
            Japanese jp1 = new Japanese("苍老师");
            Japanese jp2 = new Japanese("波多老师");
            Person pr = new Person("人类");
            Person[] pers = { cn1, cn2, jp1, jp2, pr };
            for (int i = 0; i < pers.Length; i++)
            {
                //if(pers[i] is Chinese)
                //{
                //	((Chinese)pers[i]).SayHello();
                //}
                //else if (pers[i] is Japanese)
                //{
                //	((Japanese)pers[i]).SayHello();
                //}
                pers[i].SayHello();
            }
        }
    }

    public class Person
    {
        private string _name;
        public string Name { get => _name; set => _name = value; }

        public Person(string name)
        {
            this.Name = name;
        }

        public virtual void SayHello()
        {
            Console.WriteLine("Person" + Name);
        }
    }

    public class Chinese : Person
    {
        public Chinese(string name) : base(name)
        {
        }

        public override void SayHello()
        {
            Console.WriteLine("Chinese" + Name);
        }
    }

    public class Japanese : Person
    {
        public Japanese(string name) : base(name)
        {
        }

        public override void SayHello()
        {
            Console.WriteLine("Japanese" + Name);
        }
    }

    public class English : Person
    {
        public English(string name) : base(name)
        {
        }

        public override void SayHello()
        {
            Console.WriteLine("Englishs" + Name);
        }
    }
}